//

// (  )
//

package com.tools.acceleratecore.logic.accelerate.memorydeepoptimizer.impl;

import android.content.Context;
import com.tools.acceleratecore.AccelerateCoreFactory;
import com.tools.acceleratecore.logic.accelerate.memorydeepoptimizer.intf.IMemoryAnalyzer;
import com.tools.acceleratecore.logic.accelerate.memorydeepoptimizer.intf.IMemoryDeepOptimizer;
import com.tools.acceleratecore.logic.accelerate.memorydeepoptimizer.intf.IMemoryDeepOptimizerMgr;
import com.tools.acceleratecore.logic.accelerate.memorydeepoptimizer.intf.IMemoryDeepOptimizerMgrListener;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessItem;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessMgr;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.util.UtilProcess;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTask;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskListener;
import java.util.Iterator;

public class MemoryDeepOptimizerMgr extends XObserver<IMemoryDeepOptimizerMgrListener> implements IMemoryDeepOptimizerMgr {
    private Context mContext = null;
    private boolean mInited = false;
    private IXThreadTask mIXThreadTaskInit = null;
    private IXThreadTask mIXThreadTaskMemoryDeepOptimizer = null;
    private IMemoryDeepOptimizer mIMemoryDeepOptimizer = null;
    private IMemoryAnalyzer mIMemoryAnaluzer = null;
    private IProcessMgr mIProcessMgr = null;
    private static final int VALUE_INT_TIME_DELAY_MEMORY_DEEP_OPTIMIZER = 1000;

    public MemoryDeepOptimizerMgr() {
        this.mContext = AccelerateCoreFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mIXThreadTaskInit = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
        this.mIXThreadTaskMemoryDeepOptimizer = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
        this.mIMemoryDeepOptimizer = (IMemoryDeepOptimizer)AccelerateCoreFactory.getInstance().createInstance(IMemoryDeepOptimizer.class);
        this.mIMemoryAnaluzer = (IMemoryAnalyzer)AccelerateCoreFactory.getInstance().createInstance(IMemoryAnalyzer.class);
        this.mIProcessMgr = (IProcessMgr)AccelerateCoreFactory.getInstance().createInstance(IProcessMgr.class);
    }

    public boolean init() {
        if(this.mInited) {
            return false;
        } else {
            this.mInited = true;
            this.mIXThreadTaskInit.start(new IXThreadTaskListener() {
                public void onThreadWork() {
                }

                public void onThreadComplete() {
                    synchronized(MemoryDeepOptimizerMgr.this.mListListener) {
                        Iterator var3 = MemoryDeepOptimizerMgr.this.getListListener().iterator();

                        while(var3.hasNext()) {
                            IMemoryDeepOptimizerMgrListener listener = (IMemoryDeepOptimizerMgrListener)var3.next();
                            listener.onMemoryDeepOptimizerMgrInitComplete();
                        }

                    }
                }
            });
            return true;
        }
    }

    public void startMemoryDeepOptimizeAsyn(final String strBroadcastComponent) {
        this.mIXThreadTaskMemoryDeepOptimizer.start(new IXThreadTaskListener() {
            public void onThreadWork() {
                MemoryDeepOptimizerMgr.this.startMemoryDeepOptimizeSyn(strBroadcastComponent);
            }

            public void onThreadComplete() {
                synchronized(MemoryDeepOptimizerMgr.this.mListListener) {
                    Iterator var3 = MemoryDeepOptimizerMgr.this.getListListener().iterator();

                    while(var3.hasNext()) {
                        IMemoryDeepOptimizerMgrListener listener = (IMemoryDeepOptimizerMgrListener)var3.next();
                        listener.onStartMemoryOptimizeAsynComplete();
                    }

                }
            }
        });
    }

    public void startMemoryDeepOptimizeSyn(String strBroadcastComponent) {
        this.mIProcessMgr.startScanProcessSyn();
        int nProcessItemCount = this.mIProcessMgr.getProcessItemCount();

        for(int nIndex = 0; nIndex < nProcessItemCount; ++nIndex) {
            IProcessItem iProcessItem = this.mIProcessMgr.queryProcessItem(nIndex);
            if(iProcessItem != null && iProcessItem.getProcessID() != UtilProcess.getMyPID() && !iProcessItem.isSystemApp()) {
                iProcessItem.kill();
            }
        }

        if(AccelerateCoreFactory.isLoadLib()) {
            try {
                Thread.sleep(1000L);
            } catch (Exception var5) {
                var5.printStackTrace();
            }

            this.mIMemoryDeepOptimizer.startMemoryDeepOptimizeSyn(this.mIMemoryAnaluzer.getOptimizeMemorySize(), strBroadcastComponent);
        }
    }
}
